var resizeTimeoutId;
$(window).load(function () {
    resize();
});

$(function () {
    // Various browsers will call window.resize event twice.  
    // The following prevents that and makes sure it only runs once.
    // It also makes sure that the resize code is not run until resizing is completed.
    $(window).resize(function (e) {
        window.clearTimeout(resizeTimeoutId);
        resizeTimeoutId = window.setTimeout('resize();', 10);
    });

    $('.events .main-content .button, .events .cta a').not('a.view-demo').click(function (e) {
        e.preventDefault();
        $('.registration-form').fadeIn('fast');
        $('.close-iframe').css('display','block');
    });
    
    // close the event registration when escape key is pressed.
    $(document).keyup(function(e) {
		if (e.keyCode == 27) { 
			$('.registration-form').fadeOut('fast');
			$('.close-iframe').css('display','none'); 
		}   // esc
	})
	$('.close-iframe').click(function() {
		$('.registration-form').fadeOut('fast');
		$('.close-iframe').css('display','none');
	});

    _gaq.push(function() {
      var pageTracker = _gat._getTrackerByName();
      var iframe = $('.registration-form');

      var allVars = $.getUrlVars();
      var marketoUrl = iframe.attr('src');
      if ($.getUrlVar('utm_source') != undefined)
      {
        marketoUrl += "?utm_source="+$.getUrlVar('utm_source');
        if ($.getUrlVar('utm_medium') != undefined) {
            marketoUrl += "&utm_medium="+$.getUrlVar('utm_medium');
        }
        if ($.getUrlVar('utm_term') != undefined) {
            marketoUrl += "&utm_term="+$.getUrlVar('utm_term');
        }
        if ($.getUrlVar('utm_content') != undefined) {
            marketoUrl += "&utm_content=" + $.getUrlVar('utm_content');
        }
        if ($.getUrlVar('utm_campaign') != undefined) {
            marketoUrl += "&utm_campaign=" + $.getUrlVar('utm_campaign');
        }
      }

      iframe.attr('src', pageTracker._getLinkerUrl(marketoUrl));
    });
})

function resize() {
    var windowHeight = $(this).height();
    var footerHeight = 114 - parseInt($('footer').css('padding-bottom'));
    var bodyHeight = $('body').height();
    var maxFooterWidth = 1171;
    var footerWidth = $('footer').width();
    if (windowHeight > bodyHeight) {
        $('footer').height(footerHeight + windowHeight - bodyHeight);
    }
    else {
        $('footer').height(114);
    }
    
    if (footerWidth > maxFooterWidth) {
        var padding = (footerWidth - maxFooterWidth) / 2;
        $('footer .social, footer .logo').removeAttr('style');
        var newLeftPad = parseInt($('footer .social').css('left')) + padding;
        var newRightPad = parseInt($('footer .logo').css('right')) + padding;
        $('footer .social').css('left', newLeftPad);
        $('footer .logo').css('right', newRightPad);
    }
}


$.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});